package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

//    public SysUserServiceImpl(SysUserDao sysUserDao, SysUserRoleDao sysUserRoleDao) {
//        this.sysUserDao = sysUserDao;
//        this.sysUserRoleDao = sysUserRoleDao;
//    } 等效于@Autowired

    @Override
    public List<SysUser> findUsers(SysUser sysUser) {
        return sysUserDao.selectUsers(sysUser);
    }

    @Override
    public SysUser findById(Integer id) throws ServiceException {
        //基于ID查询用户及其对应的部门信息
        SysUser sysUser=sysUserDao.selectById(id);//user dept
        if (sysUser==null)
            throw  new ServiceException("用户已不存在");
        //基于ID查询用户对应的角色ID
       List<Integer> rolesIds= sysUserRoleDao.selectRoleIdsByUserId(id);//roles
        sysUser.setRoleIds(rolesIds);
        return  sysUser;
    }

    @Transactional
    @Override
    public int saveUser(SysUser sysUser) {
        //1保存用户自身信息
        //密码加密MD5，MD5是一种不可逆的加密算法，只能加密不能解密
        String password =sysUser.getPassword();
        String salt= UUID.randomUUID().toString();
        SimpleHash simpleHash=new SimpleHash("MD5",password,salt,1);
        password=simpleHash.toHex();
        sysUser.setPassword(password);
        sysUser.setSalt(salt);
        int rows=sysUserDao.insertUser(sysUser);
        sysUserRoleDao.insertUserRoles(sysUser.getId(), sysUser.getRoleIds());
        return rows;
    }

    @Override
    public int updateUser(SysUser sysUser) throws ServiceException {
       int rows= sysUserDao.updateUser(sysUser);
       if (rows==0)
           throw new ServiceException("用户可能已经不存在");
       //更新用户和关系
       sysUserRoleDao.deleteByUserId(sysUser.getId());
       sysUserRoleDao.insertUserRoles(sysUser.getId(), sysUser.getRoleIds());
        return rows;
    }

    /**
     * @Transactional 事务切入点方法
     * @RequiresPermissions 描述方法时为一个授权切入点方法，使用时需要授权，
     * shiro框架底层会获取注解，进而取到注解中的权限标识
     * 调用subject对象的checkPermissions方法检测用户是否有权限
     */
    @RequiresPermissions("sys:user:update")
    @Transactional
    @Override
    public int validById(Integer id, Integer valid) {

       // String modifiedUser="admin";//后续是登录用户
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        String modifiedUser=sysUser.getModifiedUser();
        return sysUserDao.validById(id, valid, modifiedUser);
    }
}
